Updates the embedded attachment's document ID if loading is successful.
public bool UpdateAttachmentDocumentId {get; set;}
true to automatically update the embedded attachment's document ID if loading is successful; otherwise, false. The default value is false.
For more information, refer to DocumentFactory.LoadDocumentAttachment and Document Attachments.
using Leadtools;using Leadtools.Codecs;using Leadtools.Document.Writer;using Leadtools.Document;using Leadtools.Caching;using Leadtools.Annotations.Engine;using Leadtools.Ocr;using Leadtools.Barcode;using Leadtools.Document.Converter;public void AttachmentsExample(){// The PDF document can be one of the following:// 1. Normal PDF without attachments. Has one or more pages and no attachments.// 2. Normal PDF with attachments. Has one or more pages and one or more attachments.// 3. PDF Portfolio. Has a placeholder page and one or more attachments.// The value of LoadDocumentOptions.LoadAttachmentsMode affects loading the documents as follows:// 1. Normal PDF without attachments: Will not be affected.// 2. Normal PDF with attachments: Number of pages is not affected.// Number of attachments is:// LoadAttachmentsMode = None then 0// LoadAttachmentsMode = AsAttachments then number of attachments found in the document.// 3. PDF Portfolio: Number of pages is:// LoadAttachmentsMode = None then 1 (the placeholder page)// LoadAttachmentsMode = AsAttachments then 0 (the document has no pages).// Number of attachments is:// LoadAttachmentsMode = None then 0// LoadAttachmentsMode = AsAttachments then number of attachments found in the document.// First, load the document without attachmentsstring pdfFile = Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.pdf");string outputDirectory = Path.GetDirectoryName(LEAD_VARS.ImagesDir);Console.WriteLine($"Loading with DocumentLoadAttachmentsMode.None");var loadDocumentOptions = new LoadDocumentOptions();loadDocumentOptions.LoadAttachmentsMode = DocumentLoadAttachmentsMode.None;using (LEADDocument document = DocumentFactory.LoadFromFile(pdfFile, loadDocumentOptions)){Console.WriteLine(document.GetDocumentFileName());Console.WriteLine($"Document has {document.Pages.Count} pages");// Find out if the document is portfoliobool isPortfolio =document.Metadata.ContainsKey(LEADDocument.MetadataKey_IsPortfolio) &&bool.Parse(document.Metadata[LEADDocument.MetadataKey_IsPortfolio]);Console.WriteLine($"IsPortfolio:{isPortfolio}");// Check that everything is as expected// We should have 0 attachments since we did not instruct the toolkit to load theseDebug.Assert(document.Attachments.Count == 0);// We should have one or more pages regardless of whether the document is PDF portfolioDebug.Assert(document.Pages.Count > 0);}// Next, load the document with attachmentsConsole.WriteLine($"Loading with DocumentLoadAttachmentsMode.AsAttachments");loadDocumentOptions.LoadAttachmentsMode = DocumentLoadAttachmentsMode.AsAttachments;using (LEADDocument document = DocumentFactory.LoadFromFile(pdfFile, loadDocumentOptions)){document.AutoDeleteAttachmentsFromCache = true;Console.WriteLine($"Document has {document.Pages.Count} pages");// Find out if the document is portfoliobool isPortfolio =document.Metadata.ContainsKey(LEADDocument.MetadataKey_IsPortfolio) &&bool.Parse(document.Metadata[LEADDocument.MetadataKey_IsPortfolio]);Console.WriteLine($"IsPortfolio:{isPortfolio}");// Check that everything is as expected// We may have 0 or more attachments depending on the fileConsole.WriteLine($"Document has {document.Attachments.Count} attachments");// We should have one or more pages unless this is PDF portfolio, then it will have 0 pagesif (isPortfolio){Debug.Assert(document.Pages.Count == 0);}// Extract all the attachments into the output directoryforeach (DocumentAttachment attachment in document.Attachments){// Show info on this attachmentConsole.WriteLine($"Attachment number {attachment.AttachmentNumber} file name is '{attachment.FileName}', " +$"length is {attachment.FileLength} bytes, and ID is {attachment.DocumentId}, Is Embedded: {attachment.IsEmbedded}");// Get the attachment file name// This value may not be unique, so combine it with the attachment numberstring attachmentFileName = $"{attachment.AttachmentNumber}-{attachment.FileName}";attachmentFileName = Path.Combine(outputDirectory, attachmentFileName);// Get the attachment as a stream, we do not want to save it to the cache so pass null for that// DocumentAttachments referenceusing (Stream attachmentStream = document.Attachments.CreateAttachmentStream(attachment.AttachmentNumber, null)){if (attachmentStream != null){// Save it to the output fileusing (var stream = File.Create(attachmentFileName)){document.SaveAttachmentToCache(null);attachmentStream.CopyTo(stream);}}}var saveOptions = new SaveAttachmentToCacheOptions();saveOptions.AttachmentNumber = 0;saveOptions.UpdateAttachmentDocumentId = true;saveOptions.UploadDocumentOptions = new UploadDocumentOptions();document.SaveAttachmentToCache(saveOptions);// Or load this attachment as a LEADDocument, this might fail if the attachment is not of a valid image// or document formattry{Console.WriteLine("Loading as LEADDocument");var loadAttachmentOptions = new LoadAttachmentOptions();loadAttachmentOptions.AttachmentNumber = attachment.AttachmentNumber;loadAttachmentOptions.UpdateAttachmentDocumentId = true;using (LEADDocument attachmentDocument = document.LoadDocumentAttachment(loadAttachmentOptions)){Console.WriteLine($"Success, attachment document mime type is {attachmentDocument.MimeType} and has {attachmentDocument.Pages.Count} pages");}}catch (Exception ex){Console.WriteLine($"Error {ex.Message}");}}}}static class LEAD_VARS{public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images";}